package com.mcxx.util;

import com.google.common.base.Strings;
import dm.jdbc.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;


public class CommonUtil {
	public static boolean invalidArgs(Object... args) {
	    return Arrays.stream(args).anyMatch(arg -> {
	      if (arg == null) {
	        return true;
	      }

	      if (arg instanceof String) {
	    	  if(arg.toString().equals("\"\"")){
		         return true;
		      }else{
		    	 return Strings.isNullOrEmpty((String) arg);
		      }

	      } else if (arg instanceof Integer) {
	        return (Integer) arg <= Constant.ZERO;
	      } else if (arg instanceof Long) {
	        return (Long) arg <= Constant.ZERO;
	      } else if (arg != null) {

	    	  return false;

	      }
	      return true;
	    });
	  }

	public  static String GetNextStrSeq(String seq){
		String ss="";
		int temp=Integer.parseInt(seq);
		temp++;
		if(temp<=9){
			ss="000"+String.valueOf(temp);
		}else if(temp>9&&temp<=99){
			ss="00"+String.valueOf(temp);
		}else if(temp>99&&temp<=999){
			ss="0"+String.valueOf(temp);
		}else{
			ss=String.valueOf(temp);
		}
		return ss;
	}

	public  static String GetNextMemberStrSeq(String seq){
		String ss="";
		int temp=Integer.parseInt(seq);
		temp++;
		if(temp<=9){
			ss="0"+String.valueOf(temp);
		}else{
			ss=String.valueOf(temp);
		}
		return ss;
	}

	public  static String GetNextMemberAreaSeq(String seq){
		String ss="";
		String seq1=seq.substring(0, 12);
		String seq2=seq.substring(12);
		int temp=Integer.parseInt(seq2);
		temp++;
		if(temp>0&&temp<10){
			ss=seq1+"00000"+String.valueOf(temp);
		}else if(temp>=10&&temp<100){
			ss=seq1+"0000"+String.valueOf(temp);
		}else if(temp>=100&&temp<1000){
			ss=seq1+"000"+String.valueOf(temp);
		}else if(temp>=1000&&temp<10000){
			ss=seq1+"00"+String.valueOf(temp);
		}else if(temp>=10000&&temp<100000){
			ss=seq1+"0"+String.valueOf(temp);
		}else if(temp>=100000){
			ss=String.valueOf(temp);
		}
		return ss;
	}

	 public static String getRandomString(int length){
	     String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	     Random random=new Random();
	     StringBuffer sb=new StringBuffer();
	     for(int i=0;i<length;i++){
	       int number=random.nextInt(62);
	       sb.append(str.charAt(number));
	     }
	     return sb.toString();
	 }





//			System.out.println(getRandomString(6));;


	 public static String getObjType(String businessType,String standType,String poorType){
			String objType="";
			if(businessType.equals("110")&&!CommonUtil.invalidArgs(standType)){
				objType=standType;
			}else if(businessType.equals("130")&&!CommonUtil.invalidArgs(poorType)){
				objType=poorType;
			}
			return objType;
		}


	public static <T> List<List<T>> fixedGrouping(List<T> source, int n) {

		if (null == source || source.size() == 0 || n <= 0){
			return null;
		}

		List<List<T>> result = new ArrayList<List<T>>();

		int sourceSize = source.size();
		int size = (source.size() / n) + 1;
		for (int i = 0; i < size; i++) {
			List<T> subset = new ArrayList<T>();
			for (int j = i * n; j < (i + 1) * n; j++) {
				if (j < sourceSize) {
					subset.add(source.get(j));
				}
			}
			if (!CollectionUtils.isEmpty(subset)){
				result.add(subset);
			}
		}
		return result;
	}


	public static String removeFileSuffix(String fileName){
		if(StringUtil.isNotEmpty(fileName) && fileName.indexOf(".")>0){
			return fileName.substring(0,fileName.indexOf("."));
		}
		return fileName;
	}

}
